﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace kwzx
{
    public partial class DDS_user : Form
    {
        public DDS_user()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataSet2TableAdapters.T_USERTableAdapter tuser = new DataSet2TableAdapters.T_USERTableAdapter();
            DataSet2TableAdapters.M_user_accgrpTableAdapter useraccg = new DataSet2TableAdapters.M_user_accgrpTableAdapter();

            DataTable tb = tuser.GetDataBy_正式员工();
            for (int i = 0; i < tb.Rows.Count;i++ )
            {
                int m_accgrpid = 18;
                //18全部生产区通行   20全部生产区(除中控中化)
              int  user_id=int.Parse( tb.Rows[i]["user_id"].ToString());
              useraccg.InsertQuery(m_accgrpid, user_id);
            }

          tb = tuser.GetDataBy_访客();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                int m_accgrpid = 2;
                //18全部生产区通行   20全部生产区(除中控中化)
                int user_id = int.Parse(tb.Rows[i]["user_id"].ToString());
                useraccg.InsertQuery(m_accgrpid, user_id);
            }

           tb = tuser.GetDataBy非正式职工();
            for (int i = 0; i < tb.Rows.Count; i++)
            {
                int m_accgrpid =20;
                //18全部生产区通行   20全部生产区(除中控中化)
                int user_id = int.Parse(tb.Rows[i]["user_id"].ToString());
                useraccg.InsertQuery(m_accgrpid, user_id);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DataSet1TableAdapters.K_DeviceIssueTableAdapter k_deviceIssue = new DataSet1TableAdapters.K_DeviceIssueTableAdapter();
            DataSet2TableAdapters.DataTable1TableAdapter tuser = new DataSet2TableAdapters.DataTable1TableAdapter();
            DataSet2TableAdapters.tt_qxTableAdapter tqx = new DataSet2TableAdapters.tt_qxTableAdapter();
            DataSet2TableAdapters.M_user_accgrpTableAdapter useraccg = new DataSet2TableAdapters.M_user_accgrpTableAdapter();
            DataTable tb = tuser.GetData();
             for (int i = 0; i < tb.Rows.Count; i++)
             {
                 string userid = tb.Rows[i]["user_id"].ToString();
               string sfzh=  tb.Rows[i]["sfzh"].ToString();
              DataTable qxtb= tqx.GetData(sfzh);
              string kh = tb.Rows[i]["kh"].ToString();
            //  k_deviceIssue.InsertQuery(kh, "0", DateTime.Now, "T_USER", userid, "授权", "");

              if (qxtb.Rows.Count > 0)
              {
                  //有权限进中控室

                  int m_accgrpid = 18;  //18全部生产区通行   20全部生产区(除中控中化)
                  
                  if (useraccg.GetDataBy_m_user_accgrpid(int.Parse(userid),m_accgrpid).Rows.Count < 1)
                  {
                     
                      int user_id = int.Parse(tb.Rows[i]["user_id"].ToString());
           
                      useraccg.deletem_user_id(user_id);//删除已有权限
                      useraccg.InsertQuery(m_accgrpid, user_id);
                     
                  }
              }
              else
              {
                  //没有权限进中控室
                  int m_accgrpid = 20;  //18全部生产区通行   20全部生产区(除中控中化)
                  int ddd = useraccg.GetDataBy_m_user_accgrpid(int.Parse(userid), m_accgrpid).Rows.Count;
                  if (ddd < 1)
                  {
          
                      int user_id = int.Parse(tb.Rows[i]["user_id"].ToString());
                      useraccg.deletem_user_id(user_id);//删除已有权限
                      useraccg.InsertQuery(m_accgrpid, user_id);
                     // k_deviceIssue.InsertQuery(kh, "0", DateTime.Now, "T_USER", userid, "授权", "");
                  }
              }
             }
        }
    }
}
